﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

public class AttackScript : MonoBehaviour
{

    public GameObject attack1Prefab, attack2Prefab, attack3Prefab, attack4Prefab;
    private GameObject spawnPointHead, spawnPointLeft, spawnPointRight;
    private GameObject player;

    private bool activeAttack1, activeAttack2, activeAttack3, activeAttack4;
    private float attack1Timer, attack2Timer, attack3Timer, attack4Timer;

    public LayerMask hitLeftRight;

    private Vector3 attack3SpawnPoint;

    Image image;
    Color c;

    void Awake()
    {
        Physics2D.IgnoreLayerCollision(11, 12);
    }

    void Start()
    {
        activeAttack1 = activeAttack2 = activeAttack3 = activeAttack4 = false;
        attack1Timer = 1f;
        attack2Timer = 5f;
        attack3Timer = 5f;
        attack4Timer = 30f;

        spawnPointHead = GameObject.Find("AttackSpawnPointHead");
        spawnPointLeft = GameObject.Find("AttackSpawnPointLeft");
        spawnPointRight = GameObject.Find("AttackSpawnPointRight");

        player = GameObject.Find("Player");

        image = GetComponent<Image>();
        c = image.color;
    }

    public void Attack1()
    {
        if (!activeAttack1)
        {
            Debug.Log("Attack 1");
            Instantiate(attack1Prefab, new Vector3(spawnPointHead.transform.position.x, spawnPointHead.transform.position.y, 0), Quaternion.identity);
            activeAttack1 = true;

            StartCoroutine(WaitForTimer(attack1Timer));
        }
    }

    public void Attack2()
    {
        if (!activeAttack2)
        {
            if (player.GetComponent<PlayerControllerScript>().isGrounded)
            {
                Debug.Log("Attack 2");
                Instantiate(attack2Prefab, new Vector3(spawnPointHead.transform.position.x, spawnPointHead.transform.position.y + 0.78f, 0), Quaternion.identity);
                activeAttack2 = true;

                StartCoroutine(WaitForTimer(attack2Timer));
            }
        }
    }

    public void Attack3()
    {
        if (!activeAttack3)
        {
            Debug.Log("Attack 3");
            float distanceRight = Vector3.Distance(spawnPointRight.transform.position, GameObject.Find("RightBorder").transform.position);
            float distanceLeft = Vector3.Distance(spawnPointLeft.transform.position, GameObject.Find("LeftBorder").transform.position);
            if (distanceRight >= distanceLeft)
            {
                Debug.Log(distanceRight);
                attack3SpawnPoint = spawnPointRight.transform.position;
                attack3SpawnPoint.x += 3.1f;
                player.GetComponent<SpriteRenderer>().flipX = false;
            }
            else
            {
                Debug.Log(distanceLeft);
                attack3SpawnPoint = spawnPointLeft.transform.position;
                attack3SpawnPoint.x -= 3.1f;
                player.GetComponent<SpriteRenderer>().flipX = true;
            }

            Instantiate(attack3Prefab, new Vector3(attack3SpawnPoint.x, attack3SpawnPoint.y, 0), Quaternion.identity);
            activeAttack3 = true;

            StartCoroutine(WaitForTimer(attack3Timer));
        }
    }

    public void Attack4()
    {
        if (!activeAttack4)
        {
            Debug.Log("Attack 4");

            Instantiate(attack4Prefab, new Vector3(-2.5f, spawnPointHead.transform.position.y, 1), Quaternion.identity);
            activeAttack4 = true;

            StartCoroutine(WaitForTimer(attack4Timer));
        }
    }

    IEnumerator WaitForTimer(float waitTime)
    {
        c.a = 0.30f;
        image.color = c;

        yield return new WaitForSeconds(waitTime);

        c.a = 1;
        image.color = c;

        if (gameObject.transform.name == "AttackButton1")
        {
            activeAttack1 = false;
        }
        else if (gameObject.transform.name == "AttackButton2")
        {
            activeAttack2 = false;
        }
        else if (gameObject.transform.name == "AttackButton3")
        {
            activeAttack3 = false;
        }
        else if (gameObject.transform.name == "AttackButton4")
        {
            activeAttack4 = false;
        }
    }
}
